<template>
  <div class="login-container">

    <!--    登录页面-->
    <div v-show="isShowSkip">

      <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on"
               label-position="left">
        <!--       登录页面 -->
        <div class="title-container">
          <h3 class="title">账号 登录</h3>
        </div>

        <!--      账号 输入框-->
        <el-form-item prop="userid">
        <span class="svg-container">
<!--          显示的图片-->
          <svg-icon icon-class="user"/>
        </span>
          <!--        输入框 -->
          <el-input
            ref="username"
            v-model="loginForm.userId"
            placeholder="请输入用户的账号"
            name="userid"
            type="text"
            tabindex="1"
            auto-complete="on"
          />
        </el-form-item>

        <!--      密码输入框-->
        <el-form-item prop="password">
        <span class="svg-container">
<!--          输入密码的图片-->
          <svg-icon icon-class="password"/>
        </span>
          <el-input
            :key="passwordType"
            ref="password"
            v-model="loginForm.userPass"
            :type="passwordType"
            placeholder="请输入用户的密码"
            name="password"
            tabindex="2"
            auto-complete="on"
          />
          <span class="show-pwd" @click="showPwd">
<!--          密码的眼睛的图片-->
          <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"/>
        </span>
        </el-form-item>

        <!--      登录按钮-->
        <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;"
                   @click.native.prevent="handleLogin">登 录
        </el-button>

        <!--      点击 , 跳转 , 注册页面-->
        <div class="tips">
          <span style="margin-right:20px;" @click="isShowSkip ? isShowSkip=false : isShowSkip=true">没有账号 注册账号!</span>
        </div>
      </el-form>
    </div>
    <!--      账号 注册页面 -->
    <div v-show="!isShowSkip">
      <el-form ref="signFrom" :model="signFrom" :rules="signRules" class="login-form" auto-complete="on"
               label-position="left">

        <div class="title-container">
          <h3 class="title">账号 注册</h3>
        </div>

        <!--        昵称输入框-->
        <el-form-item prop="username">
        <span class="svg-container">
<!--          显示的图片-->
          <svg-icon icon-class="example"/>
        </span>
          <!--        输入框 -->
          <el-input
            ref="username"
            v-model="signFrom.username"
            placeholder="请输入用户的昵称,中间不能包含空格"
            name="username"
            type="text"
            tabindex="1"
            auto-complete="on"
          />
        </el-form-item>

        <!--        账号 输入框-->
        <el-form-item prop="userid">
        <span class="svg-container">
<!--          显示的图片-->
          <svg-icon icon-class="user"/>
        </span>
          <!--        输入框 -->
          <el-input
            ref="userid"
            v-model="signFrom.userid"
            placeholder="请输入要注册的账号,13-19开头的11位数字"
            name="username"
            type="text"
            tabindex="2"
            auto-complete="on"
          />
        </el-form-item>

        <!--        密码输入框-->
        <el-form-item prop="userpass">
        <span class="svg-container">
<!--          输入密码的图片-->
          <svg-icon icon-class="password"/>
        </span>
          <el-input
            :key="passwordType"
            ref="userpass"
            v-model="signFrom.userpass"
            :type="passwordType"
            placeholder="请输入用户的密码"
            name="userpass"
            tabindex="3"
            auto-complete="on"
            @keyup.enter.native="handleLogin"
          />
          <span class="show-pwd" @click="showPwd">
<!--          密码的眼睛的图片-->
          <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"/>
        </span>
        </el-form-item>


        <!--        确认密码 输入框-->
        <el-form-item prop="userpasss">
        <span class="svg-container">
<!--          输入密码的图片-->
          <svg-icon icon-class="password"/>
        </span>
          <el-input
            :key="passwordType"
            ref="userpasss"
            v-model="signFrom.userpasss"
            :type="passwordType"
            placeholder="请再次输入用户的密码"
            name="userpasss"
            tabindex="4"
            auto-complete="on"
            @keyup.enter.native="handleLogin"
          />
          <span class="show-pwd" @click="showPwd">
<!--          密码的眼睛的图片-->
          <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"/>
        </span>
        </el-form-item>


        <!--      注册按钮-->
        <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;"
                   @click="handleSign()">注 册
        </el-button>

        <!--      点击 , 跳转 , 登录页面-->
        <div class="tips">
          <span style="margin-right:20px;" @click="isShowSkip ? isShowSkip=false : isShowSkip=true">已有账号 开始登录!</span>
        </div>
      </el-form>
    </div>

  </div>
</template>

<script>
export default {
  name: 'Login',
  data() {
    //判断登录时 , 用户的账号
    const validateUsername = (rule, value, callback) => {
      if (value === '') {
        callback(new Error("账号不能为空!"))
      } else if (!/^1[3-9]\d{9}$/.test(value)) {
        callback(new Error("账号格式不正确!"))
      } else {
        callback();
      }
    };
    //判断登录时 ,用户的密码
    const validatePassword = (rule, value, callback) => {
      if (value === '') {
        callback(new Error("密码不能为空!"));
      } else if (!/^[0-9a-zA-Z]{6,18}$/.test(value)) {
        callback(new Error("密码格式不正确!"))
      } else {
        callback()
      }
    };

    //判断注册时 ,用户的昵称
    let signName = (rule, value, callback) => {
      if (value === '') {
        callback(new Error("昵称不能为空"))
      } else {
        callback()
      }
    };
    //判断注册时 ,用户的id
    let signId = (rule, value, callback) => {
      if (value === '') {
        callback(new Error("账号不能为空"))
      } else if (!/^1[3-9]\d{9}$/.test(value)) {
        callback(new Error("账号格式不正确!"))
      } else {            //=====================
        callback();
      }
    };
    //判断注册时 ,用户的确认密码
    let signPasss = (rule, value, callback) => {
      if (value === '') {
        callback(new Error("确认密码不能为空"))
      } else if (value === this.signFrom.userpassword) {
        callback();
      } else {
        callback(new Error("用户的确认密码与密码不一致"))
      }
    };
    //数据
    return {
      //判断 , 当前是登录页面 , 还是注册页面 , 指定一个boolean值进行跳转
      isShowSkip: true,

      //登录时的账号密码
      loginForm: {
        //当前登录的用户的账号
        userId: '',
        //当前登录的用户的密码
        userPass: ''
      },
      //注册时的信息
      signFrom: {
        username: '', //昵称
        userid: '',   //账号
        userpass: '', //密码
        userpasss: '', //确认密码

      },
      //指定注册时间 , 验证的格式
      loginRules: {
        userId: [{required: true, trigger: 'blur', validator: validateUsername}],
        userPass: [{required: true, trigger: 'blur', validator: validatePassword}]
      },
      //指定注册时 , 验证的格式
      signRules: {
        username: [{required: true, trigger: 'blur', validator: signName}],
        userid: [{required: true, trigger: 'blur', validator: signId}],
        userpass: [{required: true, trigger: 'blur', validator: validatePassword}],
        passpasss: [{required: true, trigger: 'blur', validator: signPasss}]
      },

      loading: false,
      passwordType: 'password',
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {

    // 显示密码/隐藏密码
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },

    //点击登录
    handleLogin() {
      this.$refs['loginForm'].validate((valid) => {
        if (valid) {
          this.loading = true
          this.$store
            .dispatch('user/login', this.loginForm)
            .then(() => {
              console.log('登录成功')
              this.$router.push({path: this.redirect || '/'})
              this.loading = false
            })
            .catch((e) => {
              console.log(e)
              this.loading = false
            })
          //本地效验账号 密码
          // this.$refs.loginForm.validate(valid => {
          //   if (valid) {
          //     this.loading = true
          //     this.$store.dispatch('user/login', this.loginForm).then(() => {
          //       this.$router.push({path: this.redirect || '/'})
          //       this.loading = false
          //     }).catch(() => {
          //       this.loading = false
          //     })
          //   } else {
          //     console.log('error submit!!')
          //     return false
          //   }
          // })
        } else {
          //  登录失败
          this.$message({message: '警告哦，登录失败!', type: 'warning'});
          return false;
        }
      })
    },

    //  注册按钮
    handleSign() {
      //  判断 验证规则 , 是否合法
      this.$refs['signFrom'].validate((valid) => {
        if (valid) {
          console.log("注册 进来了");
          this.loading = true;
          //  发送请求
          this.$store
            .dispatch("user/sign", this.signFrom)
            .then(() => {
              console.log("注册成功: , 自动登录 , 进入主页面");
              this.$router.push({path: this.redirect || '/'})
              this.loading = false
            })
            .catch((error) => {
              console.log("注册失败" + error);
              this.loading = false
            })
        } else {
          //注册失败
          this.$message({message: '警告哦，注册失败!', type: 'warning'});
          return false;
        }
      })

    }


  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
